$cmp_500 = Math_IntegerOp::compare($idx, new Math_Integer(500));
if ( ($cmp_46 > 0) && ($cmp_500 <= 0) ) {
return new Math_Integer($table[$idx->toString()]);
} else {
// calculate and cache the values
$a = new Math_Integer($table['499']);
$b = new Math_Integer($table['500']);
$pos = new Math_Integer('501');
$one = new Math_Integer('1');
while (Math_IntegerOp::compare($pos,$idx) <= 0) {
$c = Math_IntegerOp::add($a, $b);
$table[$pos->toString()] = $c->toString();
$a = $b;
$b = $c;
$pos = Math_IntegerOp::add($pos, $one);
}
return $c;
}
}/*}}}*/
/**
* Returns a series of Fibonacci numbers using the given limits.
* Method accepts two parameters, of which the second one is
* optional. If two parameters are passed, the first one will be
* lower bound and the second one the upper bound. If only one
* parameter is passed, it will be the upper bound, and the lower
* bound will be 0 (zero).
*
* @param integer $idx1 the lower index for the series (if two parameters) were passed, or the upper index if only one was given.
* @param optional integer $idx2 the upper index for the series
* @return mixed on success, an array of integers where the keys correspond to the indexes of the corresponding Fibonacci numbers, or PEAR_Error othewise
* @access public
*/
function series($idx1, $idx2=null) {/*{{{*/
if (is_integer($idx1) && $idx1 > 0) {
if ($idx2 == null) {
$lower_bound = 0;
$upper_bound = $idx1;
} elseif (is_integer($idx2)) {
if ($idx2 < 0) {
return PEAR::raiseError("Upper limit $idx2 cannot be negative");
} elseif ($idx2 < $idx1) {
return PEAR::raiseError("Upper limit cannot be smaller than lower limit");
} else {
$lower_bound = $idx1;
$upper_bound = $idx2;
}
}
$fibSeries = array();
for ($i=$lower_bound; $i <= $upper_bound; $i++) {
$fibSeries[$i] =& Math_Fibonacci::term($i);
}
return $fibSeries;
} else {
return PEAR::raiseError("The parameter $idx1 is not a valid integer");
}
}/*}}}*/
/**
* Determines if a particular integer is part of the Fibonacci series
*
* @param integer $num
* @return mixed TRUE if it is a Fibonacci number, FALSE if not, PEAR_Error if the parameter was not an integer
* @access public
* @see Math_Fibonacci::term
*/
function isFibonacci($num) {/*{{{*/
if (!Math_IntegerOp::isMath_Integer($num)) {
return PEAR::raiseError('Not a valid Math_Integer object');
}
$n = Math_Fibonacci::_estimateN($num);
$intcalc =& Math_Fibonacci::term($n);
$cmp = Math_IntegerOp::compare($num, $intcalc);
return ($cmp == 0);
}/*}}}*/
/**
* Decomposes an integer into a sum of Fibonacci numbers
*
* @param integer $num
* @return mixed an array of Fibonacci numbers on success, PEAR_Error otherwise
* @access public
*/
function decompose($num) {/*{{{*/
if (!Math_IntegerOp::isMath_Integer($num)) {
return PEAR::raiseError('Not a valid Math_Integer object');